//
//  Show360DegreeView.m
//  RotationImage
//
//  Created by wang didi on 11-12-2.
//  Copyright (c) 2011年 __MyCompanyName__. All rights reserved.
//

#import "Show360DegreeView.h"

#define IMAGE_WIDTH  720
#define IMAGE_HEIGHT 560
#define PRELOADCOUNT 18

@interface Show360DegreeView(PrivateMethod)

- (void)buildView:(CGRect)frame;

@end

@implementation Show360DegreeView

- (id)initWithFrame:(CGRect)frame  sensitive:(NSUInteger)sensitive
{
    if ( self = [super initWithFrame:frame])
    {   
        nCurImage = 0;
       
        nFingerDistance = sensitive;
        
        showedView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height)];
        [showedView setUserInteractionEnabled:TRUE];
        [self addSubview:showedView];
        [showedView release];

        degreeView = [[DegreeView alloc]initWithFrame:CGRectMake((frame.size.width-408)/2 , 543, 408, 9)];
        [self addSubview:degreeView];
        [degreeView release];
    
        navigateView = [[NavigateView alloc]initWithFrame:CGRectMake(0 , 590, frame.size.width, NAVIGATEITEMHEIGHT)];
        [self addSubview:navigateView];
        [navigateView release];
        
        operationQueue = [NSOperationQueue new];
    }
    
    return self;
}

- (void)sleep
{
    if(m_dataArray!=nil)
    {
        for(NSInteger i=0;i<[m_dataArray count];i++)
        {
            if(i!=nCurImage)
            {
              [m_dataArray replaceObjectAtIndex:i withObject:[NSNull null]];
            }
        }
    }
}

- (void)wakeup
{
    [self selectCategory:[NSNumber numberWithInt:nCurCategory]];
}

- (void)loadCarData:(NSArray*)catArray
{
    if (catArray && catArray.count >0)
    {
        SafeRelease(m_carArray);
        m_carArray = [catArray retain];
        
        [navigateView loadData:catArray];
        [navigateView selectIndex:0];
        nCurCategory = 0;
    }
}

- (void)selectCategory:(NSNumber*)index
{
    nCurCategory = [index intValue];
    NSDictionary * dic = [m_carArray objectAtIndex:nCurCategory];
    [self loadCarImage:[dic objectForKey:@"360imgs"]];
    
}


- (void)loadCarImage:(NSArray*)imgArray
{
    if (imgArray == nil) {
        return;
    }
    [operationQueue cancelAllOperations];
    
    SafeRelease(m_imgArray);
    m_imgArray = [imgArray retain];
    NSInteger imgCount = [m_imgArray count];
    
    if(m_dataArray==nil)
    {
        m_dataArray = [[NSMutableArray arrayWithCapacity:imgCount]retain];
        for(uint i=0;i<imgCount;i++)
        {
            [m_dataArray addObject:[NSNull null]];
        }
    }
    if (m_imgArray.count<nCurImage)
    {
        return;
    }
    
    UIImage *image = [[UIImage alloc]initWithContentsOfFile:[m_imgArray objectAtIndex:nCurImage]];
    if (image)
    {
        [m_dataArray replaceObjectAtIndex:nCurImage withObject:image];
        [showedView setImage:image];
        [image release];
    }
    
    [degreeView selectIndex:nCurImage withTotal:imgCount];

    NSMutableArray * indexArray = [NSMutableArray arrayWithCapacity:imgCount-1];
    for(uint i =0;i<imgCount;i++)
    {
        if(i!=nCurImage)
        {
            [indexArray addObject:[NSNumber numberWithInt:i]];
        }
    }

    for(NSNumber * number in indexArray)
    {
        [self loadCarImageAtIndex:[number intValue]];
    }
}

-(void)loadCarImageAtIndex:(NSInteger)index
{
    NSInvocationOperation *invocationOperation = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(loadDataAsync:) object:[NSNumber numberWithInt:index]];
    [operationQueue addOperation:invocationOperation];
    [invocationOperation release];
}

-(void)loadDataAsync:(NSNumber*) indexNumber
{
    NSInteger index = [indexNumber intValue];
    
    UIImage *image = [[UIImage alloc]initWithContentsOfFile:[m_imgArray objectAtIndex:index]];
    [m_dataArray replaceObjectAtIndex:index withObject:image];
    [image release];
}


- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    ptLast = [[touches anyObject] locationInView:showedView];
    SafeRelease(lasTime);
    lasTime = [[NSDate date] retain];
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    CGPoint ptCur = [[touches anyObject] locationInView:showedView];
    
    NSDate *now = [NSDate date];
    
    NSTimeInterval interval = [now timeIntervalSinceDate:lasTime];
    
    double length = ABS(ptCur.x - ptLast.x)*interval;
   
    if ( ptCur.x < ptLast.x && length >= nFingerDistance )
    {
        nCurImage = ++nCurImage > [m_imgArray count]-1 ? 0 : nCurImage;
        [showedView setImage:[m_dataArray objectAtIndex:nCurImage]];
        [degreeView selectIndex:nCurImage withTotal:[m_imgArray count]];
        ptLast = ptCur;
        SafeRelease(lasTime);
        lasTime = [now retain];
    }
    else if ( ptCur.x > ptLast.x && length >= nFingerDistance )
    {        
        nCurImage = --nCurImage < 0 ? [m_imgArray count]-1 : nCurImage;
        [showedView setImage:[m_dataArray objectAtIndex:nCurImage]];
        [degreeView selectIndex:nCurImage withTotal:[m_imgArray count]];
        ptLast = ptCur;
        SafeRelease(lasTime);
        lasTime = [now retain];
    }
}

-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    [self wakeup];
}

- (void)dealloc
{
    SafeRelease(m_carArray);
    SafeRelease(m_imgArray);
    SafeRelease(m_dataArray);
    SafeRelease(operationQueue);
    SafeRelease(lasTime);
    [super dealloc];
}

@end
